package com.naizhuai.ui.rank;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.ui.BaseFragment;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.entity.User.RankData;
import com.naizhuai.ui.entity.User.UserRank;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observer;
import rx.functions.Action1;

/**
 * @author：lichenxi
 * @date 2018/5/21 12
 * email：525603977@qq.com
 * Fighting
 */
public abstract class BaseRankFragment extends BaseFragment {
    @BindView(R.id.item_list)
    RecyclerView itemList;

    protected RankActivity mRankActivity;
    TextRecyclerViewAdapter mViewAdapter;
    Unbinder unbinder;
    private String currentDateKey = "";

    public Observer getRankObserver() {
        return mRankObserver;
    }

    private Observer mRankObserver = new NetWorkObserver<UserRank>() {
        @Override
        protected void onFail(String errormsg) {
            hideWaitDialog();
            shortToast(errormsg);
        }

        @Override
        protected void onSuccess(UserRank result) {
            if (mRankActivity != null) {
                mRankActivity.setYearData(result.dateSelect);
            }
            hideWaitDialog();
            mViewAdapter.setCategoryList(result.categoryList);
            loadFragmentData(result.OptionLabel.key, currentDateKey=result.YearLabel.key);
            showSelfData(result.selfRankData);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResID(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        itemList.addItemDecoration(new ManageDividerItemDecoration(getActivity(), ManageDividerItemDecoration.VERTICAL_LIST, 10, true));
        itemList.setAdapter(mViewAdapter=new TextRecyclerViewAdapter());
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof RankActivity) {
            mRankActivity = (RankActivity) context;
            mRankActivity.getYearSubscribe().subscribe(dateKey -> {
                //根据传递进来的年份 请求接口数据 然后展示
                loadRankData("",dateKey);
            });
        }
    }

    public class TextRecyclerViewAdapter
            extends RecyclerView.Adapter<TextRecyclerViewAdapter.ViewHolder> {
        private List<UserRank.LabelBean> categoryList;

        public void setCategoryList(List<UserRank.LabelBean> categoryList) {
            this.categoryList = categoryList;
            this.categoryList.get(0).setSelected(true);
            notifyDataSetChanged();
        }

        @Override
        public int getItemCount() {
            return categoryList!=null ?categoryList.size():0;
        }

        public TextRecyclerViewAdapter() {
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_list_content, parent, false);
            return new TextRecyclerViewAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            UserRank.LabelBean type = categoryList.get(position);
            holder.name.setText(categoryList.get(position).label);
            holder.name.setSelected(type.isSelected());
            holder.name.setOnClickListener(v -> {
                if (!type.isSelected()) {
                    holder.name.setSelected(type.toggleSelected());
                    int index = categoryList.indexOf(type);
                    for (int i = 0; i < categoryList.size(); i++) {
                        if (i != index) {
                            categoryList.get(i).setSelected(false);
                        }
                    }
                    loadFragmentData(type.key, currentDateKey);
                    notifyDataSetChanged();
                }
            });
        }
        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.name)
            TextView name;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }

    protected abstract int getLayoutResID();

    protected abstract void loadRankData(String key, String datekey);

    protected abstract void showSelfData(RankData rankData);

    protected abstract void loadFragmentData(String key, String dateKey);
}
